package com.jyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyh.common.CustomException;
import com.jyh.dto.SetmealDto;
import com.jyh.mapper.SetmealMapper;
import com.jyh.pojo.Setmeal;
import com.jyh.pojo.SetmealDish;
import com.jyh.service.SetmealDishService;
import com.jyh.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 创建套餐
     * @param setmealDto
     */
    @Override
    @Transactional
    public void createSetmealDish(SetmealDto setmealDto) {
        //获取套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //保存套餐
        this.save(setmealDto);

        Long id = setmealDto.getId();
        for (SetmealDish s :setmealDishes ) {
            s.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id查询套餐及包含菜品
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmealDish(Long id) {
        Setmeal setmeal = this.getById(id);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> dishes = setmealDishService.list(setmealDishLambdaQueryWrapper);

        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(dishes);

        return setmealDto;
    }

    /**
     * 更新数据
     * @param setmealDto
     */
    @Override
    public void updateSetmealDish(SetmealDto setmealDto) {
        //更新套餐表
        this.updateById(setmealDto);

        //套餐内菜品表
        LambdaQueryWrapper<SetmealDish> lqw=new LambdaQueryWrapper<>();
        //根据套餐id
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //删除对应菜品
        setmealDishService.remove(lqw);

        //设置套餐内菜品 对应的套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish s :setmealDishes ) {
            s.setSetmealId(setmealDto.getId());
        }
        //添加
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * （批量）删除套餐
     * @param ids
     */
    @Override
    public void deleteSetmealDish(List<Long> ids) {
        //套餐起售状态，不能删除
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.eq(Setmeal::getStatus,1);
        int count = this.count(lqw);
        if(count>0){
            throw new CustomException("套餐停售后才能删除！");
        }
        //套餐表删除
        this.removeByIds(ids);
        //套餐菜品删除
        LambdaQueryWrapper<SetmealDish> lqw2=new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw2);
    }
}
